﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Ranet.Olap.Core;
using Ranet.Olap.Core.Metadata;
using Ranet.Olap.Core.Providers;
using Ranet.Web.Olap;

namespace AnalysisApp.Web.Controllers
{
    public class OlapMetaDataController : ApiController
    {
        public AdomdConnectionPool Pool = new AdomdConnectionPool();

        public class OlapData
        {
            public MetadataQueryType queryType { set; get; }
            public ConnectionInfo connectionInfo{ set; get; }
            public string[] parameters{ set; get; }
        }
        public dynamic Post([FromBody]OlapData par)
        {

            dynamic res=null;
            try
            {



                Pool.ConnectionInformation = new ConnectionInfo(par.connectionInfo.ConnectionID,
                                                                par.connectionInfo.ConnectionString);
                OlapMetadataProvider provider = new OlapMetadataProvider(Pool.ConnectionInformation,
                                                                         Pool.CurrentConnection);

                switch (par.queryType)
                {
                    case MetadataQueryType.GetCubes:
                        res = provider.GetCubes().Where(p => p.Value.Type == CubeInfoType.Cube).ToArray();
                        break;
                    case MetadataQueryType.GetMeasures:
                        res = provider.GetMeasures(par.parameters[0]);
                        break;
                    case MetadataQueryType.GetKPIs:
                        res = provider.GetKPIs(par.parameters[0]);
                        break;
                    case MetadataQueryType.GetLevels:
                        res = provider.GetLevels(par.parameters[0], par.parameters[1], par.parameters[2]);
                        break;
                    case MetadataQueryType.GetDimensions:
                        res = provider.GetDimensions(par.parameters[0]);
                        break;
                    case MetadataQueryType.GetHierarchies:
                        res = provider.GetHierarchies(par.parameters[0], par.parameters[1]);
                        break;
                    case MetadataQueryType.GetDimension:
                        res = provider.GetDimension(par.parameters[0], par.parameters[1]);
                        break;
                    case MetadataQueryType.GetHierarchy:
                        res = provider.GetHierarchy(par.parameters[0], par.parameters[1], par.parameters[2]);
                        break;
                    case MetadataQueryType.GetMeasureGroups:
                        res = provider.GetMeasureGroups(par.parameters[0]);
                        break;
                    case MetadataQueryType.GetLevelProperties:
                        res = provider.GetLevelProperties(par.parameters[0], par.parameters[1], par.parameters[2]);
                        break;
                    case MetadataQueryType.GetCubeMetadata:
                    case MetadataQueryType.GetCubeMetadata_AllMembers:
                        res = provider.GetCubeMetadata(par.parameters[0], par.queryType);
                        break;
                }
            }
            catch (Exception e)
            {
                HttpResponseException httpResponseException=new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                                                                                          {
                                                                                              Content = new StringContent(e.Message)
                                                                                          });
              
            }
           
            return res;
        }
    }
}
